﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StoragePeriod.BLL.FoodTesting;
using StoragePeriod.BLL.RawDict;
using StoragePeriod.Model.FoodTesting;
using StoragePeriod.Model.RawDict;

namespace StoragePeriod.Web.FoodTesting.MineralsFood
{
    public partial class MineralsEdit : System.Web.UI.Page
    {
        T_FoodRecord tFoodRecord = new T_FoodRecord();  //检测记录
        T_MineralData tMineralData = new T_MineralData();              //检测记录
        T_RawMaterial tRawMaterial = new T_RawMaterial();//原料操作

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["RawID"] != null && Request.QueryString["FoodTime"] != null && Request.QueryString["FoodNum"] != null)
                {
                    this.LoadData();
                }
            }
        }

        //加载数据
        public void LoadData()
        {
            DateTime date = Convert.ToDateTime(Request.QueryString["FoodTime"].ToString());
            int RawId = Convert.ToInt32(Request.QueryString["RawID"].ToString());

            E_MineralData md_MineralData = new E_MineralData();
            md_MineralData.MineralTime = date;
            md_MineralData.RawId = RawId;
            List<E_MineralData> eMineralData = tMineralData.GetDataListByTime(md_MineralData);

            E_FoodRecord md_FoodRecord = new E_FoodRecord();
            md_FoodRecord.FoodTypeID = 5;//检测
            md_FoodRecord.FoodTime = date;
            md_FoodRecord.RawID = RawId;
            E_FoodRecord eFoodRecord = tFoodRecord.GetFoodRecord(md_FoodRecord);

            if (eFoodRecord != null)
            {
                this.labMean.Text = eFoodRecord.FoodResult;
            }

            if (eMineralData != null)
            {
                int FoodNum = int.Parse(Request.QueryString["FoodNum"]);
                E_MineralData md = eMineralData.Where(p => p.FoodNum == FoodNum).FirstOrDefault();//获取第一次检测记录
                if (md != null)
                {
                    this.txt_C1.Text = md.Mineral_C1;
                    this.txt_c2.Text = md.Mineral_C2;
                    this.txt_V.Text = md.Mineral_V;
                    this.txt_f.Text = md.Mineral_f;
                    this.txt_m.Text = md.Mineral_m;
                    this.txt_K.Text = md.Mineral_K;
                    this.txt_Ca.Text = md.Mineral_Ca;
                    this.txt_Fe.Text = md.Mineral_Fe;
                    
                }
            }
        }

        //提交数据
        protected void btPost_Click(object sender, EventArgs e)
        {
            int RawId = Convert.ToInt32(Request.QueryString["RawID"]);
            DateTime date = Convert.ToDateTime(Request.QueryString["FoodTime"]);
            int FoodNum = int.Parse(Request.QueryString["FoodNum"]);
            try
            {
                E_RawMaterial eRawMaterial = tRawMaterial.GetModel(RawId);

                double C = Convert.ToDouble(this.txt_C1.Text.Trim());
                double c = Convert.ToDouble(this.txt_c2.Text.Trim());
                double V = Convert.ToDouble(this.txt_V.Text.Trim());
                double f = Convert.ToDouble(this.txt_f.Text.Trim());
                double m = Convert.ToDouble(this.txt_m.Text.Trim());
                double Zn = ((C - c) * V * f * 100) / m * 1000;
                Convert.ToInt32(Zn);

                string K = this.txt_K.Text.Trim();
                string Ca = this.txt_Ca.Text.Trim();
                string Fe = this.txt_Fe.Text.Trim();

                E_MineralData eMineralData = new E_MineralData();
                eMineralData.RawId = RawId;
                eMineralData.RawName = eMineralData.RawName;
                eMineralData.Mineral_C1 = C.ToString();
                eMineralData.Mineral_C2 = c.ToString();
                eMineralData.Mineral_V = V.ToString();
                eMineralData.Mineral_f = f.ToString();
                eMineralData.Mineral_m = m.ToString();
                eMineralData.Mineral_Zn = Zn.ToString("f2");
                eMineralData.Mineral_K = K;
                eMineralData.Mineral_Ca = Ca;
                eMineralData.Mineral_Fe = Fe;
                eMineralData.MineralTime = date;
                eMineralData.FoodNum = FoodNum;

                tMineralData.RemoveByFoodNum(eMineralData);   //删除现有的检测记录
                tMineralData.Add(eMineralData);               //添加检测记录
                ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('提交成功！');</script>");
            }
            catch
            {
                ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('输入有误！');</script>");
            }
        }

        //计算平均值
        protected void btnMean_Click(object sender, EventArgs e)
        {
            DateTime date = Convert.ToDateTime(Request.QueryString["FoodTime"].ToString());
            int RawId = Convert.ToInt32(Request.QueryString["RawID"].ToString());

            E_MineralData md_MineralData = new E_MineralData();
            md_MineralData.MineralTime = date;
            md_MineralData.RawId = RawId;
            List<E_MineralData> eVcDataList = tMineralData.GetDataListByTime(md_MineralData);


            E_FoodRecord md_FoodRecord = new E_FoodRecord();
            md_FoodRecord.FoodTypeID = 5;//检测
            md_FoodRecord.FoodTime = date;
            md_FoodRecord.RawID = RawId;
            E_FoodRecord eFoodRecord = tFoodRecord.GetFoodRecord(md_FoodRecord);

            if (eVcDataList == null || eVcDataList.Count < 1)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('请先提交检测数据！');</script>");
                return;
            }

            double Result = 0;//检测结果
            eVcDataList.ForEach(p =>
            {
                Result += Convert.ToDouble(p.Mineral_Zn);
            });

            if (eFoodRecord != null)  //若存在则进行修改
            {
                eFoodRecord.FoodResult = (Result / eVcDataList.Count).ToString("f2");
                tFoodRecord.Update(eFoodRecord);
                this.labMean.Text = eFoodRecord.FoodResult;
            }
            else
            {
                md_FoodRecord.FoodResult = (Result / eVcDataList.Count).ToString("f2");
                E_RawMaterial eRawMaterial = tRawMaterial.GetModel(RawId);
                md_FoodRecord.RawName = eRawMaterial.RAWName;
                tFoodRecord.Add(md_FoodRecord);
                this.labMean.Text = md_FoodRecord.FoodResult;
            }
            ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('计算完毕！');parent.window.RefValue();</script>");
        }
    }
}